package com.dhp.campus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhp.campus.Common.ErrorCode;
import com.dhp.campus.Constant.ChatTypeConstant;
import com.dhp.campus.Exception.exception;
import com.dhp.campus.domain.Chat;
import com.dhp.campus.domain.User;
import com.dhp.campus.domain.request.GetPrivateChartRequest;
import com.dhp.campus.domain.vo.MessageVo;
import com.dhp.campus.domain.vo.WebSocketVo;
import com.dhp.campus.mapper.ChatMapper;
import com.dhp.campus.service.ChatService;
import com.dhp.campus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* @author Dhp
* @description 针对表【chat(聊天消息表)】的数据库操作Service实现
* @createDate 2024-03-13 14:09:02
*/
@Service
public class ChatServiceImpl extends ServiceImpl<ChatMapper, Chat>
    implements ChatService {

    @Autowired
    private UserService userService;

    @Override
    public MessageVo chatResult(Long id, Long toId, String text, Integer chatType, DateTime date) {
        WebSocketVo fromUser = new WebSocketVo();
        WebSocketVo toUser = new WebSocketVo();

        MessageVo messageVo = new MessageVo();
        messageVo.setChatType(chatType);
        messageVo.setCreateTime(String.valueOf(date));
        messageVo.setText(text);

        User from = userService.getById(id);
        User to = userService.getById(toId);
        if (BeanUtil.isEmpty(from) || BeanUtil.isEmpty(to)){
            throw new exception(ErrorCode.BUSINESS_ERROR , "操作失败");
        }
        BeanUtil.copyProperties(from , fromUser);
        BeanUtil.copyProperties(to , toUser);

        messageVo.setFormUser(fromUser);
        messageVo.setToUser(toUser);

        return messageVo;
    }

    @Override
    public List<MessageVo> getPrivateChartMessage(GetPrivateChartRequest privateChartRequest, HttpServletRequest request) {
        User currentUser = userService.getCurrentUser(request);
        if (currentUser == null){
            throw new exception(ErrorCode.NOT_LOGIN);
        }
        Long userId = currentUser.getId();
        Long toId = privateChartRequest.getToId();
        if (toId == null || toId <= 0){
            throw new exception(ErrorCode.PARAMS_ERROR  , "用户id为空或非法");
        }

        //根据ChatType和fromid，toid查询相关的消息记录
        QueryWrapper<Chat> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chat_type" , ChatTypeConstant.PRIVATE_CHAT).and(
                wrapper -> wrapper.eq("from_id" , userId)
                                  .eq("to_id" , privateChartRequest.getToId())
                                  .or().eq("to_id" , userId).eq("from_id" , toId)
                )
                ;
        List<Chat> list = this.list(queryWrapper);

        if (CollectionUtils.isEmpty(list)){
            return Collections.EMPTY_LIST;
        }

        List<MessageVo> chats = new ArrayList<>();
        User toUser = userService.getSafetyUser(userService.getById(toId));
        WebSocketVo fromUser = new WebSocketVo();
        BeanUtil.copyProperties(currentUser , fromUser);
        WebSocketVo to = new WebSocketVo();
        BeanUtil.copyProperties(toUser , to);


        for (Chat chat : list) {
            MessageVo messageVo = new MessageVo();
            //chatType , createTime , text
            BeanUtil.copyProperties(chat , messageVo);
            if (chat.getFromId().equals(userId)){
                messageVo.setIsMy(true);
            }
            messageVo.setFormUser(fromUser);
            messageVo.setToUser(to);
            chats.add(messageVo);
        }

        return chats;
    }
}




